package com.example.demo.service.impl;

import com.example.demo.globalvar.LocalCache;
import com.example.demo.listener.FileChangeListener;
import com.example.demo.mapper.FileMetadataMapper;
import com.example.demo.service.FileMetadataService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;

@Slf4j
@Service
public class FileMetaDataServiceImpl implements FileMetadataService {
    @Resource
    private ThreadPoolTaskExecutor taskExecutor;

    @Override
    public void fileChangeSave() {
        // 监视目录
        File directory = new File("E:\\textFile");

        taskExecutor.execute(() -> {
            // 创建观察者
            FileAlterationObserver observer = new FileAlterationObserver(directory);
            observer.addListener(new FileChangeListener());

            // 创建监控器，设置间隔时间（毫秒）
            FileAlterationMonitor monitor = new FileAlterationMonitor(1000);
            monitor.addObserver(observer);
            try {
                monitor.start();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

            System.out.println("开始监控目录: " + directory.getAbsolutePath());
        });
    }
}
